<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_categorycontrol extends base {

    function admin_categorycontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('category');
    }

    function ondefault() {
        $category['grade'] = $pid =0;
        $categorylist = $_ENV['category']->list_by_pid($pid);
        $category_js=$_ENV['category']->select_option_cate($id);
        include template('categorylist','admin');
    }

    function onadd() {
        if(isset($this->post['submit'])) {
            $pid = $this->post['pid'] ;
            $lines = explode("\n" , $this->post['categorys']);
            $_ENV['category']->add($lines,$pid);
            cleardir(MY_ROOT.'/data/cache'); //清除缓存文件
            $this->ondefault();
        }else {
            $id=intval($this->get[2]);
            $selectedarray=array();
            if($id) {
                $category = $this->category[$id];
                $item=$category;
                for($grade=$category['grade'];$grade>0;$grade--) {
                    $selectedarray[]=$item['id'];
                    $item['pid'] && $item=$this->category[$item['pid']];
                }
            }
            $selected_id_list=implode(' ',array_reverse($selectedarray));
            $category_js=$_ENV['category']->select_option_cate($id);
            include template('addcategory','admin');
        }
    }

    function onedit() {
        $id = (isset($this->get[2]))?$this->get[2]:$this->post['id'];
        if(isset($this->post['submit'])) {
            $name=trim($this->post['name']);
            $categorydir=trim($this->post['dir']);
            $categorydir=empty($categorydir)?getpinyin($name):$categorydir;
            $cid=$this->post['pid'];
            empty($cid) && $cid=0;
            $_ENV['category']->update_by_id($id,$name,$categorydir,$cid);
            cleardir(MY_ROOT.'/data/cache'); //清除缓存文件
            $this->post['cid']? $this->onview($this->post['cid']) :$this->ondefault();
        }else {
            $category = $this->category[$id];
            $item=$category;
            $selectedarray=array();
            for($grade=$category['grade'];$grade>1;$grade--) {
                $selectedarray[]=$item['pid'];
                $item=$this->category[$item['pid']];
            }
            $selected_id_list=implode(' ',array_reverse($selectedarray));
            $category_js=$_ENV['category']->select_option_cate($id);
            include template('editcategory','admin');
        }
    }

    //后台分类管理查看一个分类
    function onview($cid=0,$msg='') {
        $cid = $cid ? $cid : intval($this->get[2]);
        $navlist=$_ENV['category']->get_navigation($cid);//获取导航
        $category = $this->category[$cid];
        $categorylist=$_ENV['category']->list_by_cid_pid($cid,$category['pid']);//获取子分类
        $pid = $cid;
        $msg && $message=$msg;
        $category_js=$_ENV['category']->select_option_cate($cid);
        include template('categorylist','admin');
    }

    //删除分类
    function onremove() {
        if(isset($this->post['cid'])) {
            $cids = implode(",",$this->post['cid']);
            $pid = intval($this->post['hiddencid']);
            $_ENV['category']->remove($cids);
            $this->onview($pid,'分类删除成功!');
        }
    }

    /*后台分类排序*/
    function onreorder() {
        $orders = explode(",",$this->post['order']);
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $cid) {
            $_ENV['category']->order_category(intval($cid),$order);
        }
        $this->cache->remove('category');
    }
    function onsyscate($msg='')
    {
    	$categorylist = $_ENV['category']->list_by_sysid();
    	$msg && $message=$msg;
        include template('catesyslist','admin');
    }
    function onaddsyscate()
    {
    	if(!empty($this->post['catename'])) {
            $stype = (int)$this->post['stype'] ;
            $_ENV['category']->addsyscate($this->post['catename'],$stype);
            $this->onsyscate('添加成功!');
        }else{
        	$this->onsyscate('分类名称不能为空!');
        }
    }
    function oncatelists($id=0,$msg='')
    {
    	$pid = ($id>0)?$id:$this->get[2];
    	$categorylist = $_ENV['category']->catelist_byid($pid);
    	$msg && $message=$msg;
    	$topname = $_ENV['category']->get_syscatename($pid);
        include template('catelist','admin');
    }
    function onupcate(){
    	if(isset($this->post['submit'])) 
    	{
    		$cid = $this->post['hiddencid'] ;
            //$lines = explode("\n" , $this->post['cname']);
            $_ENV['category']->upcid($this->post['cname']);
            $this->oncatelists($cid,"编辑成功");
    	}
    }
    function onaddcate()
    {
    	if(isset($this->post['submit'])) 
    	{
    		$cid = $this->post['cid'] ;
            $lines = explode("\n" , $this->post['categorys']);
            $_ENV['category']->addcid($lines,$cid);
            $this->oncatelists($cid,"添加成功");
    	}
    	else 
    	{
	    	$id = (isset($this->get[2]))?$this->get[2]:$this->post['id'];
	    	$cate_js = $_ENV['category']->get_syscate($id);
	    	$topname = $_ENV['category']->get_syscatename($id);
	    	include template('cateadd','admin');
    	}
    }
    function ondelcate()
    {
    	$id = (int)$this->get[2];
    	$cid = (int)$this->get[3];
    	$_ENV['category']->delcid($id);
        $this->oncatelists($cid,"删除成功");
    }
    function oneditsyscate()
    {
    	$id = (isset($this->get[2]))?$this->get[2]:$this->post['id'];
    	if(isset($this->post['submit'])) 
    	{
    		$name = $this->post['name'] ;
    		$stype = (int)$this->post['stype'] ;
    		 $_ENV['category']->updatesyscate($name,$stype,$id);
            $this->onsyscate("编辑成功");
    	}
    	else 
    	{
	    	
	    	//$cate_js = $_ENV['category']->get_syscate($id);
	    	$d = $_ENV['category']->get_syscate_byid($id);
	    	//var_dump($d);
	    	include template('catesysedit','admin');
    	}
    }
    function onreordercate()
    {
    	$orders = explode(",",$this->post['order']);
    	//var_dump($orders);exit();
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $cid) {
            $_ENV['category']->order_cate(intval($cid),$order);
        }
    }
}
?>